package ilog

import (
	"strings"

	"github.com/go-kratos/kratos/v2/log"
)

type (
	Level      string
	LevelValue int
)

const (
	TraceLevel Level = "trace"
	DebugLevel Level = "debug"
	InfoLevel  Level = "info"
	WarnLevel  Level = "warn"
	ErrorLevel Level = "error"
	FatalLevel Level = "fatal"
	PanicLevel Level = "panic"
)

const (
	TraceLevelValue LevelValue = iota - 1
	DebugLevelValue
	InfoLevelValue
	WarnLevelValue
	ErrorLevelValue
	FatalLevelValue
	PanicLevelValue
)

func (l LevelValue) Enable(level LevelValue) bool {
	return level >= l
}

func ParseLevel(level Level) LevelValue {
	switch level {
	case TraceLevel:
		return TraceLevelValue
	case DebugLevel:
		return DebugLevelValue
	case InfoLevel:
		return InfoLevelValue
	case WarnLevel:
		return WarnLevelValue
	case ErrorLevel:
		return ErrorLevelValue
	case FatalLevel:
		return FatalLevelValue
	case PanicLevel:
		return PanicLevelValue
	default:
		return InfoLevelValue
	}
}

func LevelFromKlog(l log.Level) Level { return Level(strings.ToLower(l.String())) }

type OutType string

const (
	Stdout OutType = "stdout"
	File   OutType = "file"
	ES     OutType = "es"
	DB     OutType = "db"
	Mongo  OutType = "mongo"
	Mix    OutType = "mix"
	Null   OutType = "null"
)

type Format string

const (
	JsonFormat Format = "json"
	TextFormat Format = "text"
)

type Config struct {
	Engine    string  `json:"engine"` // logroute / zap / ...
	Level     Level   `json:"level"`
	Path      string  `json:"path"`
	FileName  string  `json:"fileName"`
	MaxSize   int     `json:"maxSize"`   // MB
	MaxAge    int     `json:"maxAge"`    // days
	MaxBackup int     `json:"maxBackup"` // 备份数
	Format    Format  `json:"format"`    // json or text
	WriteOut  OutType `json:"writeOut"`  // 输出类型
	CallSkip  int     `json:"callSkip"`  // skip call depth
}

func StdoutConfig() Config {
	return Config{
		Level:    DebugLevel,
		Format:   TextFormat,
		WriteOut: Stdout,
		CallSkip: 5,
		Engine:   "zap",
	}
}
